﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Reflection;
using System.Windows.Forms;
using System.Net;
using System.IO;
using System.Threading;
using System.Net.Sockets;
using System.Text;
namespace SenrobControl
{
    public class IPCam
    {
        Panel outpanel;
        byte[] buffer;
        Stream str;
        string url;
        int size = 150000;

        public IPCam(string url, string login, string password)
        {
            this.url = url;
            
            this.outpanel = new Panel();
            this.outpanel.Size = new Size(320, 240);

            connect(url, login, password);
        }

        private void connect(string url, string login, string password)
        {
            HttpWebRequest req = (HttpWebRequest)WebRequest.Create("http://146.232.146.90/mjpg/video.mjpg");
            req.Credentials = new NetworkCredential(login, password);
            HttpWebResponse resp = (HttpWebResponse)req.GetResponse();
            str = resp.GetResponseStream();

            this.buffer = new byte[size];
        }

        public Image GetJPG()
        {
            int r = this.str.Read(buffer, 0, size);
            string read = Encoding.ASCII.GetString(buffer);
            
            // remove header
            int offset = read.LastIndexOf("\n")+1;
            
            int len = read.Length;

            if (MessageBox.Show(read, "txt", MessageBoxButtons.OKCancel) == DialogResult.Cancel) Environment.Exit(0);

            return Bitmap.FromStream(new MemoryStream(buffer, 0, r));
        }
    }
}
